import { defineStore } from 'pinia'
import axios from 'axios'

interface Course {
  id: string
  title: string
  description: string
  instructor: string
  coverImage: string
  price: number
  level: string
  duration: string
  studentCount: number
  rating: number
  chapters: Chapter[]
  materials: Material[]
}

interface Chapter {
  id: string
  title: string
  lessons: Lesson[]
}

interface Lesson {
  id: string
  title: string
  description: string
  duration: string
  url: string
}

interface Material {
  id: string
  name: string
  url: string
}

interface Comment {
  id: string
  username: string
  avatar: string
  rating: number
  content: string
  date: string
}

interface CourseProgress {
  courseId: string
  lessonId: string
  progress: number
}

interface CourseFilters {
  page: number
  category?: string
  level?: string
  search?: string
}

export const useCourseStore = defineStore('course', {
  state: () => ({
    courses: [] as Course[],
    currentCourse: null as Course | null,
    loading: false,
    error: null as string | null
  }),

  actions: {
    async getCourses(filters: CourseFilters) {
      try {
        this.loading = true
        const response = await axios.get('/api/courses', { params: filters })
        return {
          courses: response.data.courses,
          totalPages: response.data.totalPages
        }
      } catch (error) {
        this.error = '获取课程列表失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    async getCategories() {
      try {
        const response = await axios.get('/api/courses/categories')
        return response.data
      } catch (error) {
        this.error = '获取课程分类失败'
        throw error
      }
    },

    async getCourseDetail(courseId: string) {
      try {
        this.loading = true
        const response = await axios.get(`/api/courses/${courseId}`)
        this.currentCourse = response.data
        return response.data
      } catch (error) {
        this.error = '获取课程详情失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    async getCourseComments(courseId: string) {
      try {
        const response = await axios.get(`/api/courses/${courseId}/comments`)
        return response.data
      } catch (error) {
        this.error = '获取课程评论失败'
        throw error
      }
    },

    async getRecommendedCourses() {
      try {
        const response = await axios.get('/api/courses/recommended')
        return response.data
      } catch (error) {
        this.error = '获取推荐课程失败'
        throw error
      }
    },

    async getLearningProgress() {
      try {
        const response = await axios.get('/api/courses/progress')
        return response.data
      } catch (error) {
        this.error = '获取学习进度失败'
        throw error
      }
    },

    async updateProgress(progress: CourseProgress) {
      try {
        await axios.post('/api/courses/progress', progress)
      } catch (error) {
        this.error = '更新学习进度失败'
        throw error
      }
    },

    async enrollCourse(courseId: string) {
      try {
        await axios.post(`/api/courses/${courseId}/enroll`)
      } catch (error) {
        this.error = '课程报名失败'
        throw error
      }
    }
  }
})